{extend name="../../common/view/admin_base"/}

{block name="main"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <button class="layui-btn layui-btn-warm">权限列表</button>
            <div class="layui-btn-group layui-form">
                {if key_exists('admin/auth/add',$btns)}
                <a class="layui-btn openfrm" data-href="{:url('admin/auth/add')}"><i class="layui-icon layui-icon-add"></i>增加</a>
                {/if}
            </div>
            <table class="layui-table layui-form" id="tree-table" lay-size="sm"></table>
        </div>
    </div>
</div>

<script type="text/html" id="checkboxTpl">
    <input type="checkbox" disabled name="close" lay-skin="switch" lay-text="是|否" {{ d.ismenu == 1 ? 'checked' : '' }} >
</script>

<script type="text/html" id="actionTpl">
    {if key_exists('admin/auth/edit',$btns)}
    <a class="layui-btn  layui-btn-xs openfrm" data-href="{:url('admin/auth/edit')}?id={{d.guid}}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    {/if}
    {if key_exists('admin/auth/remove',$btns)}
    <a class="layui-btn layui-btn-danger layui-btn-xs removebtn" data-href="{:url('admin/auth/remove')}?id={{d.guid}}"><i class="layui-icon layui-icon-delete"></i>删除</a>
    {/if}
</script>

<script>
    layui.config({
        base: '__static__/layui/extend/'
    }).extend({
        treeTable:'treeTable'
    }).use(['treeTable','layer','code','form','element','laytpl'],function(){
            var form = layui.form,
                layer = layui.layer,
                treeTable = layui.treeTable,
                tree = null;
            var laytpl = layui.laytpl;

            $.post("{:url('admin/auth/index')}?type=list",function(res){
                tree = treeTable.render({
                    elem: '#tree-table',
                    data: res.data,
                    icon_key: 'name',
                    primary_key: 'guid',
                    parent_key: 'pid',
                    is_checkbox: true,
                    is_cache: false,
                    checked: {
                        key: 'guid',
                        data: [],
                    },
                    end: function(e){
                        $("#tree-table .layui-icon-triangle-r").removeClass('layui-icon-triangle-r').addClass('layui-icon-triangle-d');
                        $("#tree-table tr.layui-hide").removeClass('layui-hide');
                        form.render();
                        $.bindactions();
                    },
                    cols: [
                        {
                            key: 'name', title: '名称',width: '200px',
                            template:function(item){
                                return '<a style="cursor:pointer;">'+item.name+'</a>';
                            }
                        },
                        {
                            key: 'icon', title: '图标',width: '260px',
                            template:function(item){
                                return '<i class="'+item.icon+'" style="margin-right:5px;"></i>'+item.icon;
                            }
                        },
                        {
                            key: 'action', title: '路径',width: '260px'
                        },
                        {
                            key: 'sort', title: '排序',width: '60px'
                        },
                        {
                            title: '是否菜单', width: '100px', align: 'center',
                            template: function(item){
                                var tmp = "";
                                laytpl($("#checkboxTpl").html()).render(item,function(html){
                                    tmp = html;
                                });
                                return tmp;
                            }
                        },
                        {
                            title: '操作', align: 'left', width:'220px',
                            template: function(item){
                                var tmp = "";
                                laytpl($("#actionTpl").html()).render(item,function(html){
                                    tmp = html;
                                });
                                return tmp;
                            }
                        },
                        {
                            title:'',
                            template:function(item){
                                return '';
                            }
                        }
                    ]
                });
            });

            $.bindactions = function(){
                $(".openfrm").on('click',function(){
                    var href = $(this).data('href');
                    top.layer.open({
                        type: 2
                        ,title:'编辑框'
                        ,content: href
                        ,area: ['750px', '520px']
                    });
                });

                $(".removebtn").on('click',function(){
                    var url = $(this).data('href');
                    top.layer.confirm("确定删除吗？此操作不能撤销！", {icon: 3, title:'提示'},
                        function(index){
                            top.layer.closeAll();
                            $.post(url,function(res){
                                layer.msg(res.msg,{
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                },function(){
                                    document.location.reload();
                                });
                            });
                        },function (index) {
                            top.layer.close(index);
                        }
                    );
                });
            }

        })
</script>
{/block}